﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.IO;
using SmartIMS.Core;

namespace System.Web.Mvc.Html
{
    public static class DataGridHelper
    {
        public static string DataGrid<T>(this HtmlHelper helper)
        {
            return DataGrid<T>(helper, null, null, "", 0, 0);
        }

        public static string DataGrid<T>(this HtmlHelper helper, object data)
        {
            return DataGrid<T>(helper, data, null, "", 0, 0);
        }

        public static string DataGrid<T>(this HtmlHelper helper, object data, GridColumns[] columns,string idColumn, int page, int recordPerPage)
        {
            //Get items
            var items = (IEnumerable<T>)data;
            if (items == null)
                items = (IEnumerable<T>)helper.ViewData.Model;

            //Get column names 
            if (columns == null)
            {
                string[] vColumns = typeof(T).GetProperties().Select(p => p.Name).ToArray();
                columns = new GridColumns[vColumns.Length];
                for (int i = 0; i < vColumns.Length; i++)
                    columns[i].Name = vColumns[i];
            }

            string strHtml = "<table border=\"0\" style=\"height:100%; width:100%\" class=\"list_table2\" cellpadding=\"0\" cellspacing=\"0\">";
            strHtml = strHtml + "<thead>";
            strHtml = strHtml + CreateHeader(helper, columns);
            strHtml = strHtml + "</thead>";

            strHtml = strHtml + "<tbody>";
            int count = 0;
            foreach (var item in items)
            {
                strHtml = strHtml + CreateRow<T>(helper, columns, item, idColumn);
                count++;
            }
            strHtml = strHtml + CreateRowEmpty<T>(helper, columns, recordPerPage - count);
            strHtml = strHtml + "</tbody>";

            strHtml = strHtml + "</table>";
            return strHtml;
        }

        private static string CreateHeader(HtmlHelper helper, GridColumns[] columns)
        {
            string strHtml = "";
            strHtml = strHtml + "<tr class=\"header\">";
            foreach (var columnName in columns)
            {
                strHtml = strHtml + "<td>";                                
                strHtml = strHtml + columnName.Title;
                strHtml = strHtml + "</td>";
            }
            strHtml = strHtml + "</tr>";
            return strHtml;
        }

        public static string CreateRow<T>(HtmlHelper helper, GridColumns[] columns, T item, string idColumn)
        {
            string strHtml = "";
            var valueIdColumn = typeof(T).GetProperty(idColumn).GetValue(item, null) ?? String.Empty;
            strHtml = strHtml + "<tr class=\"odd_row\" >";
            foreach (var columnName in columns)
            {
                
                var value = typeof(T).GetProperty(columnName.Name).GetValue(item, null) ?? String.Empty;
                if (columnName.Type == "CheckBox")
                {
                    strHtml = strHtml + "<td>";
                    strHtml = strHtml + helper.CheckBox(columnName.Name, new { value = value.ToString(), id = columnName.Name + value.ToString()});
                }
                else
                {
                    strHtml = strHtml + "<td style='cursor:pointer;' onclick=\"$('#" + idColumn + "').val('" + valueIdColumn + "');$('form').attr('action', '/Listtype/Detail/View');$('form').submit(this.submit);\">";
                    strHtml = strHtml + helper.Encode(value.ToString());
                }
                strHtml = strHtml + "</td>";
            }
            strHtml = strHtml + "</tr>";
            return strHtml;
        }

        public static string CreateRowEmpty<T>(HtmlHelper helper, GridColumns[] columns, int count)
        {
            string strHtml = "";
            for (var i = 0; i < count; i++)
            {
                strHtml = strHtml + "<tr class=\"odd_row\">";            
                foreach (var columnName in columns)
                {
                    strHtml = strHtml + "<td>";
                    strHtml = strHtml + "&nbsp;";
                    strHtml = strHtml + "</td>";
                }
                strHtml = strHtml + "</tr>";
            }
            return strHtml;
        }
    }
}
